# -*- coding: utf-8 -*-
'''
Created on 2013-5-12

@author: yq
'''
from Common.htmlHlep import Find
from Common.tool import tool
from Cms import config
from Cms.plus import  page,user
from Models import  models
import  hashlib,datetime,random
from django.db import transaction
from AdminMain.ArticleCommon import  ArticleCommon

@page.page_render(user.Admin())
def AddImg(request):
        info=models.Article_Content()
        info.ContentId=tool.guid()
        return Find.View(request,info)
    
@page.page_render(user.Admin())
@transaction.commit_manually 
def DoAddImg(request):
    try:
        info=ArticleCommon().Add(request,2)
        info.save()
        transaction.commit()
    except Exception:
        transaction.rollback()
        
    return Find.JsonX(False, "操作成功")
    
@page.page_render(user.Admin())
@transaction.commit_manually 
def DoAddNewImg(request):
    try:
        Img=models.Article_Image()
        Find.UpdateModel(request,Img)
        Img.ImageId=tool.guid()
        Img.TopNum=0
        contentId=request.POST.get("ContentId","")
        Img.ContentId=models.Article_Content.objects.get(ContentId=contentId)
        Img.save()
        transaction.commit()
    except Exception,e:
        print e
        transaction.rollback()
        
    return Find.JsonX(False, "操作成功")

@page.page_render(user.Admin())
@transaction.commit_manually 
def DoDelNewImg(request):
    try:
        ImageId=request.GET.get("ImageId","")
        Img=models.Article_Image.objects.get(ImageId=ImageId)
        if Img.FileId!="" or Img.FileId is not None :
            models.Sys_File.objects.filter(FileId=Img.FileId).delete()
        Img.delete()
        transaction.commit()
    except Exception,e:
        print e
        transaction.rollback()
        
    return Find.JsonX(False, "操作成功")



@page.page_render(user.Admin())
def Edit(request):
        contentId=request.GET.get("ContentId","")
        info=models.Article_Content.objects.get(ContentId=contentId)
        Img=models.Article_Image()
        
        return Find.View(request,{"info":info,"Img":Img})
    
@page.page_render(user.Admin())
@transaction.commit_manually 
def DoEdit(request):
        '''
                            编辑新闻
        '''
        try:
            info=ArticleCommon().Edit(request)
            
            #更新图片文件
            models.Sys_File.objects.filter(ModelId=info.ContentId).update(IsBind=1)
            info.save()
            transaction.commit()
        except Exception,e:
            transaction.rollback()
            print e
            return Find.JsonX(True, "操作失败")
        return Find.JsonX(False, "操作成功")
    
    
    
@page.page_render(user.Admin())
def List(request):
    '''
           列表
    '''
    list=models.Article_Image.objects.filter(ContentId=request.POST.get("KeyId","")).order_by("TopNum")
    return Find.View(request, {"List":list,"KeyId":request.POST.get("KeyId","")})

@page.page_render(user.Admin())
def DoMobile(request):
        '''
                            移动到
        '''
        
        dataOrderList=str(request.POST.get("dataOrderid")).split(',')
        i=1
        for key in dataOrderList:
            info=models.Article_Image.objects.get(ImageId=key,ContentId=request.POST.get("KeyId",""))
            if info.TopNum!=i:
                info.TopNum=i
                info.save()
            i=i+1
        
        return Find.JsonX(False, "操作成功")
    
    